home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5633 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: tbird2.pad.otc.com.au!grahamd
  2. From: grahamd@nms.otc.com.au (Graham Dumpleton)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: delete[] and template questions
  5. Date: 5 Feb 1996 22:48:16 GMT
  6. Organization: Telstra, Network Management Systems, Paddington, Australia
  7. Message-ID: <4f61fg$73@tbird2.pad.otc.com.au>
  8. References: <1996Jan30.165654.2033@iiasa.ac.at> <4enump$qlr@fsuj01.rz.uni-jena.de>
  9. NNTP-Posting-Host: baldric.pad.otc.com.au
  10. X-Newsreader: NN version 6.5.0 #1 (NOV)
  11.  
  12. mkt@isun04.inf.uni-jena.de (Tilo Koerbs) writes:
  13.  
  14. >> template <class I, class T>
  15. >> void mVect<I,T>::resize(I new_size) {
  16. >>   T *old = v;
  17. >>   v = new T[new_size];        // T *v is a private member of mVect
  18. >>    int size_of_elem = sizeof(T);        // <--- question 3
  19. >>   delete[] old;        // <--- questions 1 & 2
  20. >> }
  21. >>
  22. >> I have the following questions:
  23. >> 1. Is it absolutely robust and portable to delete[] old, even
  24. >>   if a previous call was for new_size == 0 (or if v was allocated
  25. >>     by the ctor for the size == 0) ?
  26. >> 2. Is it correct to assume that no destructor is called by this statement ?
  27. >> 3. Is there any risk involved in using sizeof(T) in this statement ?
  28.  
  29. >1. If you really called new T[x] regardless if x == 0,
  30. >    or if old is a NULL pointer
  31. >    than the answer is YES!
  32.  
  33. Technically "YES", in reality, not always.
  34.  
  35. The ATT/USL 3.0.1 compiler, if the class T has a virtual destructor, has
  36. been seen to core dump when deleting a vector when the size of the vector
  37. was originally zero. Purify picked up that the compiler was accessing past
  38. the end of the single byte array which had been allocated to satisfy the
  39. requirement that allocating an array of size zero still return a valid
  40. pointer.
  41.  
  42. Also, some versions of the ATT/USL compiler have been seen to core dump
  43. when you use delete[] on a null pointer.
  44.  
  45. Thus, the answer depends on how robust and portable you want to make your
  46. code.
  47.  
  48.  
  49. --
  50. Graham Dumpleton (grahamd@nms.otc.com.au)
  51.